package com.dzp.simpleSyslogServer.runner;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.dzp.simpleSyslogServer.common.CommonPool;
import com.dzp.simpleSyslogServer.syslog.SyslogServer_dzp;
import com.dzp.simpleSyslogServer.thread.TaskThread;

/**
 * 项目启动后 默认执行方法
 * 
 * @author dzp
 *
 */
@Component
@Order(value = 1) // 执行顺序控制
public class MyApplicationRunner implements ApplicationRunner {
	private static Logger logger = LoggerFactory.getLogger(MyApplicationRunner.class);
	@Value("${simpleSyslogServer.port_tcp}")
	private Integer port_tcp;
	@Value("${simpleSyslogServer.port_udp}")
	private Integer port_udp;

	/**
	 * 初始化配置
	 */
	@Override
	public void run(ApplicationArguments applicationArguments) throws Exception {
		logger.info("-------MyApplicationRunner 初始化执行---------");
		// 启动tcp sysLog 服务
		if (port_tcp > 0) {
			JSONObject params = new JSONObject();
			params.put("type", "SyslogServer");
			params.put("protocol", SyslogServer_dzp.syslog_protocol_tcp);
			params.put("port", port_tcp);
			CommonPool.executeTask(new TaskThread(params));
		}
		// 启动udp sysLog 服务
		if (port_udp > 0) {
			JSONObject params = new JSONObject();
			params.put("type", "SyslogServer");
			params.put("protocol", SyslogServer_dzp.syslog_protocol_udp);
			params.put("port", port_udp);
			CommonPool.executeTask(new TaskThread(params));
		}

	}
}